import { ENUM_CLIP, ENUM_STATUS, ENUM_UI } from "../GbzEnum";
import GbzInstance from "../GbzInstance";
import { timerFormat } from "../GbzUtils";
import GbzAudioManager from "../manager/GbzAudioManager";
import GbzDataManager from "../manager/GbzDataManager";
import BaseLayer from "./GbzBaselayer";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GbzMainLayer extends BaseLayer {

    btn_exit: cc.Node = null
    timer_label: cc.Label = null
    timer_index: number = -1

    skills: cc.Node = null

    levelNum: cc.Node = null

    onLoad() {
        this.btn_exit = cc.find('btn_exit', this.node)
        this.btn_exit.on('click', this.onExit, this)
        const timer = cc.find('timer', this.node)
        this.timer_label = timer.getComponent(cc.Label)

        this.skills = cc.find('skills', this.node)
        this.skills.children.forEach((skill, index) => {
            if (!skill.hasEventListener('click')) {
                skill.on('click', () => {
                    this.onSkillClick(index)
                })
            }
        })

        this.levelNum = this.node.getChildByName('level_num')
    }

    onDestroy() {
        this.btn_exit.off('click', this.onExit, this)
    }

    onExit() {
        if (GbzDataManager.ins.status == ENUM_STATUS.UNRUN) return
        GbzAudioManager.ins.play(ENUM_CLIP.GAME_CLICK)
        GbzInstance.uiMgr.toggle(ENUM_UI.LAYER_EXIT)
    }

    startCountDown() {
        this.timer_label.string = timerFormat(`${GbzDataManager.ins.data_timer}`, 'i:s')
        this.timer_label.schedule(() => {
            if (GbzDataManager.ins.data_timer <= 1) this.stopCountDown()
            GbzDataManager.ins.data_timer -= 1
            GbzDataManager.ins.data_time_count += 1
            if (GbzDataManager.ins.data_timer < 0) GbzDataManager.ins.data_timer = 0
            this.timer_label.string = timerFormat(`${GbzDataManager.ins.data_timer}`, 'i:s')
            this.setCountDownSound(true)
            if (GbzDataManager.ins.data_timer <= 0) {
                this.setCountDownSound(false)
                GbzInstance.gameMgr.onGameOver(ENUM_UI.LAYER_LOSE_TIME)
            }
        }, 1)
    }

    stopCountDown() {
        this.timer_label.unscheduleAllCallbacks()
        this.setCountDownSound(false)
    }

    async setCountDownSound(bool: boolean) {
        if (bool) {
            if (GbzDataManager.ins.data_timer <= 5 && GbzDataManager.ins.data_timer > 0 && this.timer_index < 0) {
                this.timer_index = await GbzAudioManager.ins.play(ENUM_CLIP.GAME_TIMER, true)
            }
        } else {
            GbzAudioManager.ins.stop(this.timer_index)
            this.timer_index = -1
        }
    }

    onSkillClick(index: number) {
        if (GbzDataManager.ins.status == ENUM_STATUS.UNRUN) return
        GbzAudioManager.ins.play(ENUM_CLIP.GAME_CLICK)
        GbzDataManager.ins.skillIndex = index
        GbzInstance.uiMgr.toggle(ENUM_UI.LAYER_TIP)
    }

    rendorCurrentLevel() {
        this.levelNum.getComponent(cc.Label).string = `第${GbzDataManager.ins.game_level-1}关`
    }
}
